package com.sq.universe.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sq.universe.base.controller.BaseController;
import com.sq.universe.base.entity.RespEntity;
import com.sq.universe.system.entity.RoleEntity;
import com.sq.universe.system.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/role")
@CrossOrigin
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    /**
    * 查询数据
    * @param roleEntity
    * @return
    */
    @PostMapping("/query")
//    @RequiresPermissions("system:role:query")
    public RespEntity query(@RequestBody(required = false) RoleEntity roleEntity){
        try {
            return new RespEntity("查询成功",roleService.query(roleEntity));
        }catch (Exception exception){
            return new RespEntity("查询失败");
        }
    }

    /**
    * 分页查询
    * @param roleEntity
    * @return
    */
    @PostMapping("/queryByPage")
    @RequiresPermissions("system:role:queryByPage")
    public RespEntity queryByPage(@RequestBody(required = false) RoleEntity roleEntity){
        try {
            PageHelper.startPage(roleEntity.getPageNo(),roleEntity.getPageSize());
            List<RoleEntity> roleList = roleService.query(roleEntity);
            PageInfo<RoleEntity> pageInfo = new PageInfo<RoleEntity>(roleList);
            return new RespEntity("查询成功",pageInfo);
        }catch (Exception exception){
            return new RespEntity("查询失败");
        }
    }

    /**
    * 插入
    * @param roleEntity
    * @return
    */
    @PostMapping("/insert")
    @RequiresPermissions("system:role:insert")
    public RespEntity insert(@RequestBody(required = false) RoleEntity roleEntity){
        try {
            roleEntity.setRoleCreateTime(new Date());
            roleEntity.setRoleCreateUser(this.getCurrentUser().getUserId());
            roleService.insert(roleEntity);
            return new RespEntity("保存成功");
        }catch (Exception exception){
            return new RespEntity("保存失败");
        }
    }

    /**
    * 更新
    * @param roleEntity
    * @return
    */
    @PostMapping("/update")
    @RequiresPermissions("system:role:update")
    public RespEntity update(@RequestBody(required = false) RoleEntity roleEntity){
        try {
            roleEntity.setRoleUpdateTime(new Date());
            roleEntity.setRoleUpdateUser(this.getCurrentUser().getUserId());
            roleService.update(roleEntity);
            return new RespEntity("更新成功");
        }catch (Exception exception){
            return new RespEntity("更新失败");
        }
    }

    /**
    * 删除成功
    * @param roleEntity
    * @return
    */
    @PostMapping("/delete")
    @RequiresPermissions("system:role:delete")
    public RespEntity delete(@RequestBody(required = false) RoleEntity roleEntity){
        try {
            if(roleEntity.getRoleId() == null){
                return new RespEntity("删除失败");
            }
            roleEntity.setRoleUpdateTime(new Date());
            roleEntity.setRoleUpdateUser(this.getCurrentUser().getUserId());
            roleService.logicalDelete(roleEntity);
            return new RespEntity("删除成功");
        }catch (Exception exception){
            return new RespEntity("删除失败");
        }
    }
}